﻿using System.Text;
using System.Text.RegularExpressions;

namespace JT809.GpsRedisConsumer.Common
{
    public static class HexExtensions
    {
        /// <summary>
        /// 16进制字符串转16进制数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static byte[] HexStringToBytes(string hexStr)
        {
            if (string.IsNullOrEmpty(hexStr))
            {
                return Array.Empty<byte>();
            }

            if (hexStr.StartsWith("0x"))
            {
                hexStr = hexStr.Remove(0, 2);
            }

            var count = hexStr.Length;

            if (count % 2 == 1)
            {
                throw new ArgumentException("Invalid length of bytes:" + count);
            }

            var byteCount = count / 2;
            var result = new byte[byteCount];
            for (int ii = 0; ii < byteCount; ++ii)
            {
                var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
                result[ii] = tempBytes;
            }

            return result;
        }

        /// <summary>
        /// 转化bytes成16进制的字符
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string BytesToHexString(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 转化bytes成16进制的字符
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string BytesToHexString(Span<byte> bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 账号换算
        /// 对第一位出现的0进行替换，如0303000402-》330402
        /// 针对账号出现|、H、L、W、X、Y、Z情况的转换
        /// 例如：HYYD330424001005010000123|1；LYZH201130321801；WX330424001005010000123|1
        /// 0|-》7C，0H-》48，0L-》4C，0W-》57，0X-》58，0Y-》59，0Z-》5A
        /// 字母只会出现在前4位，竖线只会出现在倒数第2位
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static string CompressHexString(string hexStr)
        {
            if (string.IsNullOrEmpty(hexStr))
            {
                return "";
            }
            List<string> result = new();
            var hs = hexStr.AsSpan();
            for (int i = 0; i < hs.Length; i += 2)
            {
                if (hs.Slice(i, 1).ToString() == "0")
                {
                    result.Add(hs.Slice(i + 1, 1).ToString());
                }
                else
                {
                    if (i <= 4)
                    {
                        var header = hs.Slice(i, 2).ToString();
                        result.Add(header switch { "48" => "H", "4C" => "L", "57" => "W", "58" => "X", "59" => "Y", "5A" => "Z", _ => header });
                    }
                    else if (i >= hs.Length - 4 && hs.Slice(i, 2).ToString() == "7C")
                    {
                        result.Add("|");
                    }
                }
            }

            return string.Join("", result.ToArray());
        }

        /// <summary>
        /// 返回字符串每两位补一个空格
        /// </summary>
        /// <param name="MSG"></param>
        /// <returns></returns>
        public static string SendFormat(string MSG)
        {
            if (string.IsNullOrEmpty(MSG))
            {
                return "";
            }
            return Regex.Replace(MSG, @"(\S{2}(?=[^$]))", "$1 ");
        }
    }
}
